#include <basic_types.h>
#include <Mediatypes.h>	
#include <api/libc/alloc.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>

#include <hld/hld_dev.h>
#include <hld/vbi/vbi.h>
#include <hld/vbi/vbi_dev.h>



/*
 * 	Name		:   vbi_open()   	
 *	Description	:   Open a vbi device
 *	Parameter	:	struct vbi_device *dev		: Device to be openned
 *	Return		:	INT32 						: Return value
 *
 */
__ATTRIBUTE_REUSE_ 
INT32 vbi_open(struct vbi_device *dev)
{
	INT32 result=RET_FAILURE;
	
	/* If openned already, exit */
	if (dev->flags & HLD_DEV_STATS_UP)
	{
		PRINTF("vbi_open: warning - device %s openned already!\n", dev->name);
		return RET_SUCCESS;
	}
	
	/* Open this device */
	if (dev->open)
	{
		result = dev->open(dev);
	}
	
	/* Setup init work mode */
	if (result == RET_SUCCESS)
	{
		dev->flags |= (HLD_DEV_STATS_UP | HLD_DEV_STATS_RUNNING);
		
	}
	
	return result;
}

/*
 * 	Name		:   vbi_close()   	
 *	Description	:   Close a vbi device
 *	Parameter	:	struct vbi_device *dev		: Device to be closed
 *	Return		:	INT32 						: Return value
 *
 */
INT32 vbi_close(struct vbi_device *dev)
{
	INT32 result=RET_FAILURE;

	if ((dev->flags & HLD_DEV_STATS_UP) == 0)
	{
		PRINTF("vbi_close: warning - device %s closed already!\n", dev->name);
		return RET_SUCCESS;
	}
	
	/* Stop device */
	if (dev->close)
	{
		result = dev->close(dev);
	}
	
	/* Update flags */
	dev->flags &= ~(HLD_DEV_STATS_UP | HLD_DEV_STATS_RUNNING);
	
	return result;
}


/*
 * 	Name		:   vbi_io_control()   	
 *	Description	:   vbiel IO control command
 *	Parameter	:	struct sto_device *dev		: Device
 *					INT32 cmd					: IO command
 *					UINT32 param				: Param
 *	Return		:	INT32 						: Result
 *
 */
INT32 vbi_ioctl(struct vbi_device *dev, UINT32 cmd, UINT32 param)
{
	/* If device not running, exit */
	if ((dev->flags & HLD_DEV_STATS_UP) == 0)
	{
		return RET_FAILURE;
	}
	
	if (dev->ioctl)
	{
		return dev->ioctl(dev, cmd, param);
	}
	
	return RET_FAILURE;
}

INT32 vbi_request_write(void * pdev, UINT32 uSizeRequested,void ** ppuData,UINT32* puSizeGot,struct control_block* pDataCtrlBlk)
{
       struct vbi_device *dev = (struct vbi_device *)pdev;
	/* If device not running, exit */
	if ((dev->flags & HLD_DEV_STATS_UP) == 0)
	{
		return RET_FAILURE;
	}
	
	if (dev->request_write)
	{
		return dev->request_write(dev, uSizeRequested, pDataCtrlBlk,(UINT8 **)ppuData,puSizeGot);
	}
	return RET_FAILURE;
}

void vbi_update_write(void * pdev, UINT32 uDataSize)
{
      struct vbi_device *dev = (struct vbi_device *)pdev;
	/* If device not running, exit */
	
	if ((dev->flags & HLD_DEV_STATS_UP) == 0)
	{
		return ;
	}
	
	if (dev->update_write)
	{
		dev->update_write(dev, uDataSize);
	}
}
void vbi_setoutput(struct vbi_device *dev,T_VBIRequest *pVBIRequest)
{
	if ((dev->flags & HLD_DEV_STATS_UP) == 0)
	{
		return ;
	}

	if (dev->setoutput)
	{
		dev->setoutput(dev, pVBIRequest);
	}
}

INT32 vbi_start(struct vbi_device *dev,t_TTXDecCBFunc pCBFunc)
{
	/* If device not running, exit */
	if ((dev->flags & HLD_DEV_STATS_UP) == 0)
	{
		return RET_FAILURE;
	}
	
	if (dev->start)
	{
		return dev->start(dev,pCBFunc);
	}
	
	return RET_FAILURE;
}
INT32 vbi_stop(struct vbi_device *dev)
{
	/* If device not running, exit */
	if ((dev->flags & HLD_DEV_STATS_UP) == 0)
	{
		return RET_FAILURE;
	}
	
	if (dev->stop)
	{
		return dev->stop(dev);
	}
	
	return RET_FAILURE;
}

INT32 ttx_request_page(struct vbi_device *dev, UINT16 page_id , struct PBF_CB ** cb )
{	

	/* If device not running, exit */
	if ((dev->flags & HLD_DEV_STATS_UP) == 0)
	{
		return RET_FAILURE;
	}
	
	if (dev->request_page)
	{
		return dev->request_page(dev, page_id,cb);
	}
	return RET_FAILURE;

}
INT32 ttx_request_page_up(struct vbi_device *dev,UINT16 page_id , struct PBF_CB ** cb )
{	
	/* If device not running, exit */
	if ((dev->flags & HLD_DEV_STATS_UP) == 0)
	{
		return RET_FAILURE;
	}
	
	if (dev->request_page_up)
	{
		return dev->request_page_up(dev, page_id,cb);
	}
	return RET_FAILURE;

}
INT32 ttx_request_page_down(struct vbi_device *dev, UINT16 page_id , struct PBF_CB ** cb )
{	
	/* If device not running, exit */
	if ((dev->flags & HLD_DEV_STATS_UP) == 0)
	{
		return RET_FAILURE;
	}
	
	if (dev->request_page_down)
	{
		return dev->request_page_down(dev, page_id,cb);
	}
	return RET_FAILURE;

}
void ttx_default_g0_set(struct vbi_device *dev, UINT8 default_g0_set)
{
	/* If device not running, exit */
	if ((dev->flags & HLD_DEV_STATS_UP) == 0)
	{
		return ;
	}
	
	if (dev->default_g0_set)
	{
		dev->default_g0_set(dev, default_g0_set);
	}
}
